package com.android.yummycookies.asyncTask;

import java.lang.ref.WeakReference;
import java.util.Set;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.android.yummycookies.model.commonModel.ObserversPool;
import com.android.yummycookies.model.theme.Theme;
import com.android.yummycookies.storage.BitmapStorage;
import com.android.yummycookies.util.Logger;

public class LoadThemeThread extends Thread {

	public static final String TAG = LoadThemeThread.class.getSimpleName();
	
//	public static int START_LOADING_THEME...
	

	private Theme themeToLoad;
	
	/*****************************************************************
	 * Contexte de persistance
	 *****************************************************************/

	/** Context applicatif */
	private final Context context;
	/** BitmapStorage applicatif */
	private final BitmapStorage bitmapStorage;
	
	/*****************************************************************
	 * Configuration du redimensionnement
	 *****************************************************************/

	/** valeur de la hauteur du redimensionnement */
	private final int heightScale;
	/** valeur de la largeur du redimensionnement */
	private final int widthScale;
	
	/*****************************************************************
	 * Collections representant l'avancement du chargement des types de cookies
	 *****************************************************************/

	/** Ensemble des identifiants des bitmap en cours de chargement */
	private final Set<Integer> loadingBitmaps;
	/** Ensemble des identifiants des bitmap en cours de redimensionnement */
	private final Set<Integer> scalingBitmaps;

	/******************************************************************
	 * Gestion des messages
	 ******************************************************************/
	private final ObserversPool observersPool = new ObserversPool();
	
	public LoadThemeThread(Context context, BitmapStorage bitmapStorage,
			Set<Integer> loadingBitmaps, Set<Integer> scalingBitmaps,
			int widthScale, int heightScale, Theme themeToLoad) {

		this.context = context;
		this.bitmapStorage = bitmapStorage;
		this.loadingBitmaps = loadingBitmaps;
		this.scalingBitmaps = scalingBitmaps;
		this.widthScale = widthScale;
		this.heightScale = heightScale;
		
		this.themeToLoad = themeToLoad;

		Logger.logDebug(TAG, TAG + " created");
	}
	
	public LoadThemeThread(Context context, BitmapStorage bitmapStorage,
			Set<WeakReference<Handler>> observers,
			Set<Integer> loadingBitmaps, Set<Integer> scalingBitmaps,
			int widthScale, int heightScale, Theme themeToLoad) {

		this.context = context;
		this.bitmapStorage = bitmapStorage;
		this.observersPool.addAllObservers(observers);
		this.loadingBitmaps = loadingBitmaps;
		this.scalingBitmaps = scalingBitmaps;
		this.widthScale = widthScale;
		this.heightScale = heightScale;
		
		this.themeToLoad = themeToLoad;

		Logger.logDebug(TAG, TAG + " created");
	}
	
	
	public ObserversPool getObserversPool() {
		return observersPool;
	}
	
	@Override
	public void start() {
//		sendStartMessage();
		
		Logger.logDebug(TAG, "Load all bitmaps", themeToLoad.getBitmapsId());
		LoadBitmapThread lbt = new LoadBitmapThread(
				context,
				bitmapStorage, loadingBitmaps, themeToLoad.getBitmapsId());
		lbt.getObservablePool().addAllObservers(observersPool);
		lbt.getObservablePool().addObserver(new LoadingHandler());
		
		lbt.start();
	}
	
	
	public void scaleBitmaps(){
		
	}
	
	private class LoadingHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LoadBitmapThread.END_LOADING_BITMAPS:
				scaleBitmaps();
				break;

			default:
				break;
			}
		}
	}
	private LoadingHandler loadingHandler = new LoadingHandler();
	
	
	private Handler scalingHandler = new Handler(){
		public void handleMessage(Message msg) {
			
		};
	};
	
}
